package cn.yoyo.service;

import cn.yoyo.entity.Hot;
import cn.yoyo.entity.LargeFile;
import cn.yoyo.mapper.HotMapper;
import cn.yoyo.mapper.LargeFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Service
public class HotServiceImpl implements HotService {
    @Autowired(required = false)
    HotMapper hotMapper;
    @Autowired(required = false)
    LargeFileMapper largeFileMapper;

    @Override
    public List<Hot> findAll() {
        return hotMapper.findAll();
    }

    @Override
    public int delete(Integer id) {
        return hotMapper.delete(id);
    }

    @Override
    @Transactional
    public int addHot(Hot hot, MultipartFile file) {
        // 给hot的图片设一个UUID后把这个照片存到图片库里
        hot.setPicture(UUID.randomUUID().toString());
        try {
            largeFileMapper.add(new LargeFile(hot.getPicture(), file.getOriginalFilename(), file.getBytes()));
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return hotMapper.addHot(hot);
    }

}
